﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LinkCard.Dao;
using LinkCard.Model;
using LinkCard.Utils;

namespace LinkCard.ShopCP.Reports
{
    public partial class export_by_customer : System.Web.UI.Page
    {
        public string sFromDate = "";
        public string sToDate = "";
        public string sCardNumber = "";
        public DateTime dFromDate = new DateTime(1900, 1, 1);
        public DateTime dToDate = new DateTime(2099, 1, 1);

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["USER"] == null)
            {
                Response.Redirect("/login.aspx");
            }

            if (!Page.IsPostBack)
            {
                if (Request.QueryString["FromDate"] != null) { sFromDate = Request.QueryString["FromDate"].Trim(); }
                if (Request.QueryString["ToDate"] != null) { sToDate = Request.QueryString["ToDate"].Trim(); }
                if (Request.QueryString["CardNumber"] != null) { sToDate = Request.QueryString["CardNumber"].Trim(); }

                LoadPayment();

                Response.Clear();
                Response.Buffer = false;
                Response.AddHeader("content-disposition", "attachment; filename=" + DateTime.Now.ToString("yyyyMMdd") + "Report.xls");
                Response.ContentType = "application/vnd.ms-excel";
                Response.Charset = "UTF-8";
            }
        }
        private void LoadPayment()
        {
            var db = new TopLinkEntities();

            Int64 nSupplierID = getSupplierID();
            
            System.Globalization.DateTimeFormatInfo dateInfo = new System.Globalization.DateTimeFormatInfo();
            dateInfo.ShortDatePattern = "MM/dd/yyyy";

            if (sFromDate != "")
            {
                sFromDate += " 00:00:00";
                dFromDate = Convert.ToDateTime(sFromDate, dateInfo);

            }
            if (sToDate != "")
            {
                sToDate += " 23:59:59";
                dToDate = Convert.ToDateTime(sToDate, dateInfo);
            }

            var _paylog = (from payment in db.PayLog
                           from customer in db.Customers
                           //where(t=> t.CustomerID == payment.CustomerID && payment.SupplierID == nSupplierID).ToList();
                           where (customer.CustomerID == payment.CustomerID
                                 && payment.SupplierID == nSupplierID
                                 && payment.PayDate >= dFromDate
                                 && payment.PayDate <= dToDate
                                 && customer.CardNumber.Contains(sCardNumber))
                           select new
                           {
                               customer.CustomerID,
                               customer.CardNumber,
                               customer.Fullname,
                               payment.PayID,
                               payment.PayDate,
                               payment.PayAmount,
                               payment.PayMethod
                           }).OrderBy(t => t.PayDate);

            var paylog = _paylog.ToList();

            if (paylog.Count > 0)
            {
                msgList.DataSource = paylog;

                msgList.DataBind();
                msgNoData.Visible = false;
            }
            else
            {

                msgList.DataSource = null;
                msgList.DataSourceID = null;
                msgList.DataBind();
                msgNoData.Visible = true;
            }

        }
        protected Int64 getSupplierID()
        {
            try
            {

                if (Convert.ToInt32(Session["SHOPADMIN"]) == 1) // quanr ly cua hang
                {
                    return ConvertUtility.ToInt64(Session["USER"]);
                }
                else if (Convert.ToInt32(Session["SHOPADMIN"]) == 0)
                {
                    DataTable dt = LinkCard.AdminCP.Common.DataHelper.SelectTable("SELECT SupplierID FROM [ShopStaff] WHERE Username ='" + Session["USER"].ToString() + "'", false);
                    if (dt.Rows.Count > 0)
                    {
                        return ConvertUtility.ToInt64(dt.Rows[0]["SupplierID"]);
                    }
                    else
                    {
                        return 0;
                    }
                }
                else
                {
                    return 0;
                }
            }
            catch
            {
                return 0;
            }
        }
    }
}